package com.example.demo.test;

public class Thread_DeadLock {
    public static void main(String[] args) {

        Dress dress = new Dress();
        Trousers trousers = new Trousers();
        //t1,t2共享dress和trousers
        Thread t1 = new Thread(new MyRunnable1(dress, trousers), "t1");
        Thread t3 = new Thread(new MyRunnable3(dress, trousers), "t1");
        t1.start();
        t3.start();
    }
}

class MyRunnable1 implements Runnable {
    Dress dress;
    Trousers trousers;

    public MyRunnable1() {
    }

    public MyRunnable1(Dress dress, Trousers trousers) {
        this.dress = dress;
        this.trousers = trousers;
    }

    @Override
    public void run() {
        synchronized (dress) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            synchronized (trousers) {
                System.out.println("--------------------------");
            }
        }

    }
}

class MyRunnable3 implements Runnable {

    Dress dress;
    Trousers trousers;

    public MyRunnable3() {
    }

    public MyRunnable3(Dress dress, Trousers trousers) {
        this.dress = dress;
        this.trousers = trousers;
    }

    @Override
    public void run() {
        synchronized(trousers){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (dress){
                System.out.println("。。。。。。。。。。。。。。");
            }
        }
    }
}

class Dress {

}

class Trousers {

}
